package com.team7.happycommunity.businessservice.mq;

import com.rabbitmq.client.Channel;
import com.team7.happycommunity.businessservice.util.SendSms;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@Service
public class PhoneCode {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SendSms sendSms;
    @Value("${smsCode}")
    private String smsCode;
    @Value("${param}")
    private String param;
    @RabbitHandler
    @RabbitListener(queues = "phone_code_queue")
    public void processPay(@Payload Map msg, Channel channel, @Headers Map headers){
        String phone = (String) msg.get("phone");
        String code = (String)msg.get("code");
      //调用阿里云地址,发送短信
     sendSms.sms(phone,smsCode,param.replace("[value]",code));
        //签收
        Long tag=(Long)headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(tag,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
